package com.sunny.myproject.core.base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * @author sunny_nie
 *
 */
public class DateUtil {

	/**
	 * 获取一个月的第一天
	 * @param date
	 * @return
	 */
	public static Date getFirtDateOfMonth(String date){
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(formateStringDate(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}
	
	/**
	 * 获取一个月的最后一天
	 * @param date
	 * @return
	 */
	public static Date getEndDateOfMonth(String date){
		Calendar calendar = Calendar.getInstance();
		try {
			Date start = DateUtil.getFirtDateOfMonth(date);
			calendar.setTime(start);
			calendar.add(Calendar.MARCH, 1);
			calendar.set(Calendar.DATE, -1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return calendar.getTime();
	}
	
	/**
	 * 日期 yyyy-MM-dd格式化
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date formateStringDate(String date) throws ParseException{
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.parse(date);
	}
	
	public static String formatDateString(Date date){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return format.format(date);
	}
}
